<template>
    <section>
        <CommonCRUD ref="table" :columns="columns" api-root="identity/cadreTask" :formColumns="formColumns"
                    :addBtnVis=false :editBtnVis=false :lookBtnVis = false :delBtnVis=false>
            <template slot="header-btn" slot-scope="slotProps">
                <el-button type="primary" plain @click="add" > 发布任务</el-button>
                <el-button type="success" plain  @click="edit(slotProps.selected)">编辑</el-button>
                <el-button type="info" plain @click="look(slotProps.selected)">查看</el-button>
                <el-button type="danger" plain  @click="del(slotProps.selected)">删除</el-button>
                <el-button type="primary" plain  @click="forck(slotProps.selected)">进度追踪</el-button>
            </template>
            <template slot="Handle" slot-scope="scope">
                <el-tag close-transition :type="new Date(scope.row.endTime) >= new Date() ? '' : 'success'"  effect="dark">{{new Date(scope.row.endTime) >= new Date() ? '进行中' : '已完成'}}</el-tag>
            </template>

        </CommonCRUD>

        <el-dialog
            v-if="dialogVisible"
            :title="title"
            :visible.sync="dialogVisible"
            width="880px"
            align="left"
            :modal-append-to-body='false'
            :append-to-body="true"
            :before-close="handleClose">
            <el-form :inline="true" :model="form"  :rules="rules" ref="form" class="demo-form-inline" label-width="170px">
                <el-form-item label="标题" prop="name">
                    <el-input v-model="form.name" :disabled="disabled"></el-input>
                </el-form-item>
                <el-form-item label="类型"  prop="type">
                    <el-select v-model="form.type" :disabled="disabled">
                        <el-option v-for="item in typeList" :value="item.value" :label="item.label" :key="item.value"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="得分" prop="score">
                    <el-input v-model="form.score" :disabled="disabled"></el-input>
                </el-form-item>
                <el-form-item label="截止时间" prop="endTime">
                    <el-date-picker v-model="form.endTime" type="date" placeholder="选择日期" value-format="yyyy-MM-dd" :disabled="disabled" clearable>
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="内容" >
                    <el-input v-model="form.content" :cols="90" type="textarea" :rows="4" :disabled="disabled"></el-input>
                </el-form-item>
                <el-form-item label="附件" >
                    <CommonFileUpload :value="form.attach" @getValue="form.attach = $event" :disabled="disabled"></CommonFileUpload>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer footer-position">
                <el-button type="primary" :loading="submitLoading" @click="submit(form)">确 认</el-button>
                <el-button @click="handleClose">取 消</el-button>
            </div>
        </el-dialog>

        <el-dialog
            v-if="forckVis"
            title="任务追踪"
            :visible.sync="forckVis"
            width="880px"
            align="left"
            :modal-append-to-body='false'
            :append-to-body="true"
            :before-close="handleForckClose">
            <CommonCRUD ref="forckTable" :columns="forckColumns" api-root="identity/cadreTaskObject" :formColumns="forckColumns"
                        :queryFormColumns="forckQueryColumns" :sortColumns="forckSortColumns"
                        :addBtnVis=false :editBtnVis=false :lookBtnVis = false :delBtnVis=false>
                <template slot="Handle" slot-scope="scope">
                    <el-tag v-show="scope.row.status == 0" close-transition type="danger"  effect="dark">待编辑</el-tag>
                    <el-tag v-show="scope.row.status == 1" close-transition type="primary"  effect="dark">已编辑,待提交</el-tag>
                    <el-tag v-show="scope.row.status == 2" close-transition type="warning" effect="dark">已提交,待镇审核</el-tag>
                    <el-tag v-show="scope.row.status == 3" close-transition type="warning"  effect="dark">镇党委审核通过,待市委审核</el-tag>
                    <el-tag v-show="scope.row.status == 4" close-transition type="success"  effect="dark">市委审核通过,任务完成</el-tag>
                </template>
            </CommonCRUD>
        </el-dialog>

    </section>
</template>

<script>
    import CommonCRUD from '@/components/CommonCRUD';
    import CommonFileUpload from '@/components/FileUpLoad';
    export default {
        name: "TaskRelease",
        data(){
            return{
                columns:[],
                formColumns:[],
                dialogVisible:false,
                title:'',
                form:{},
                submitLoading:false,
                typeList:[
                    {
                        label:"基本信息更新",
                        value:"基本信息更新"
                    },
                    {
                        label:"年度考核",
                        value:"年度考核"
                    },
                    {
                        label:"职级评定",
                        value:"职级评定"
                    }
                ],
                rules:{
                    name: [
                        { required: true, message: '请输入标题', trigger: 'blur' }
                    ],
                    type: [
                        { required: true, message: '请选择类型', trigger: 'change' }
                    ],
                    score: [
                        { required: true, message: '请输入得分', trigger: 'blur' }
                    ],
                    endTime: [
                        { required: true, message: '请选择截止时间', trigger: 'blur'}
                    ],
                },
                forckVis: false,
                forckColumns: [],
                forckSortColumns: [{
                    name: 'townName',
                    type: 'desc'
                }],
                forckQueryColumns: [
                    {
                        des: '名称',
                        name: 'taskId',
                        type: 'string',
                        value: '',
                        visible: false,
                    }, {
                        des: '镇级组织',
                        name: 'townName',
                        type: 'string',
                        value: '',
                        visible: true,
                    }
                ]
            }
        },
        methods:{
            add(){
                this.title = "发布";
                this.dialogVisible = true;
                this.disabled = false;
                this.form={};
            },
            edit(data){
                if (data.length !== 1) {
                    this.$message({
                        type: 'warning',
                        message: data.length > 1 ? '仅能选择一行记录' : '请选择一行记录'
                    });
                    return;
                }
                this.title = "编辑";
                this.dialogVisible = true;
                this.disabled = false;
                this.form = data[0];
            },
            look(data){
                if (data.length !== 1) {
                    this.$message({
                        type: 'warning',
                        message: data.length > 1 ? '仅能选择一行记录' : '请选择一行记录'
                    });
                    return;
                }
                this.title = "查看";
                this.dialogVisible = true;
                this.disabled = true;
                this.form = data[0];
            },
            del(data){
                if (data.length !== 1) {
                    this.$message({
                        type: 'warning',
                        message: data.length > 1 ? '仅能选择一行记录' : '请选择一行记录'
                    });
                    return;
                }
                this.$confirm('确认删除？')
                    .then(_ => {
                        this.$http("DELETE", `identity/cadreTask/${data[0].id}id`).then(_ => {
                            this.$refs.table.refreshTableData();
                        });
                    })
                    .catch(_ => {});
            },
            forckColumnsInit() {
                this.forckColumns = [{
                    //绑定的属性名
                    name: "taskName",
                    // 后台字段可能存在别名
                    aliasName: null,
                    // Label的名称
                    des: "任务名称",
                    // 字段类型
                    type: "string",
                    // 字段类型是否校验（校验）
                    typeCheck: null,
                    // 表单是否展示
                    formShow: 'true',
                    // 表格是否展示（默认展示）
                    notShow: 'false',
                    // 该字段是否是对象
                    isObject: "0",
                    // 字段长度
                    length: 50,
                    // 最大值（校验）
                    max: null,
                    //  最小值（校验）
                    min: null,
                    obj: null,
                    objectId: null,
                    // 是否必填（校验）
                    required: null,
                    // 字段排序
                    sort: 1,
                    // 如果是数据字典，字典的Key
                    lookupKey: null,
                    // 数据字典的转换类型
                    transferType: null,
                    // 失去焦点触发验证（校验）
                    triggerCheck: null,
                },{
                    //绑定的属性名
                    name: "townName",
                    // 后台字段可能存在别名
                    aliasName: null,
                    // Label的名称
                    des: "镇级组织",
                    // 字段类型
                    type: "string",
                    // 字段类型是否校验（校验）
                    typeCheck: null,
                    // 表单是否展示
                    formShow: 'true',
                    // 表格是否展示（默认展示）
                    notShow: 'false',
                    // 该字段是否是对象
                    isObject: "0",
                    // 字段长度
                    length: 50,
                    // 最大值（校验）
                    max: null,
                    //  最小值（校验）
                    min: null,
                    obj: null,
                    objectId: null,
                    // 是否必填（校验）
                    required: null,
                    // 字段排序
                    sort: 1,
                    // 如果是数据字典，字典的Key
                    lookupKey: null,
                    // 数据字典的转换类型
                    transferType: null,
                    // 失去焦点触发验证（校验）
                    triggerCheck: null,
                },{
                    //绑定的属性名
                    name: "objectName",
                    // 后台字段可能存在别名
                    aliasName: null,
                    // Label的名称
                    des: "村级组织",
                    // 字段类型
                    type: "string",
                    // 字段类型是否校验（校验）
                    typeCheck: null,
                    // 表单是否展示
                    formShow: 'true',
                    // 表格是否展示（默认展示）
                    notShow: 'false',
                    // 该字段是否是对象
                    isObject: "0",
                    // 字段长度
                    length: 50,
                    // 最大值（校验）
                    max: null,
                    //  最小值（校验）
                    min: null,
                    obj: null,
                    objectId: null,
                    // 是否必填（校验）
                    required: null,
                    // 字段排序
                    sort: 1,
                    // 如果是数据字典，字典的Key
                    lookupKey: null,
                    // 数据字典的转换类型
                    transferType: null,
                    // 失去焦点触发验证（校验）
                    triggerCheck: null,
                }];
            },
            submit (form) {
                this.$refs.form.validate((valid) => {
                    if (valid) {
                        this.submitLoading = true;
                        //新增
                        if (form.id == null) {
                            this.$http('POST', `/identity/cadreTask/`, form).then(() => {
                                this.submitLoading = false;
                                this.dialogVisible = false;
                                this.$refs.table.refreshTableData();
                                this.form = {};
                            }).catch(() =>{
                                this.submitLoading = false;
                            });
                        }
                        //编辑
                        if ((form.id != null) && (this.disabled === false)) {
                            this.$http('PUT', `identity/cadreTask/${form.id}id`, form).then(() => {
                                this.submitLoading = false;
                                this.dialogVisible = false;
                                this.$refs.table.refreshTableData();
                                this.form = {};
                            }).catch(() =>{
                                this.submitLoading = false;
                            });
                        } else {//查看
                            this.submitLoading = false;
                            this.dialogVisible = false;
                            this.form = {};
                        }
                    } else {
                        return false;
                    }
                });
            },
            //关闭确认dialog
            handleClose (done) {
                this.$confirm('确认关闭？')
                    .then(_ => {
                        this.from = {};
                        this.$refs['form'].resetFields();
                        this.disabled = false;
                        this.dialogVisible = false;
                        done();
                    })
                    .catch(_ => {});
            },
            //追踪关闭确认dialog
            handleForckClose (done) {
                this.$confirm('确认关闭？')
                    .then(_ => {
                        this.forckVis = false;
                        done();
                    })
                    .catch(_ => {});
            },
            // 进度追踪
            forck(data) {
                if (data.length !== 1) {
                    this.$message({
                        type: 'warning',
                        message: data.length > 1 ? '仅能选择一行记录' : '请选择一行记录'
                    });
                    return;
                }
                this.forckVis = true;
                this.forckQueryColumns[0].value = data[0].id;
            }
        },
        components:{
            CommonCRUD,
            CommonFileUpload
        },
        created() {
            this.columns = [];
            let temp = JSON.parse(JSON.stringify(this.$store.state.classInfo.properties));
            this.columns = temp;
            this.columns.push({slot: true, name: 'state', des: '状态', slotName: 'Handle'});

            this.forckColumnsInit();
            this.forckColumns.push({slot: true, name: 'state', des: '状态', slotName: 'Handle'});
        }
    }
</script>

<style scoped>

</style>
